# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None


class Solution:
    def rotateRight(self, head: ListNode, k: int) -> ListNode:
        length = 0
        
        li = []
        h = head
        while (h is not None):
            length += 1
            h = h.next
        if length == 0:
            return head
        if k % length == 0:
            return head
        count = length - (k % length)
        h = head
        for i in range(count):
            h = h.next
        t = h
        while(t is not None):
            li.append(t.val)
            t = t.next
        t = head
        while(t != h):
            li.append(t.val)
            t = t.next
        begin = ListNode(li[0])
        bb = begin
        for i in range(1,length):
            bb.next = ListNode(li[i])
            bb = bb.next
        return begin

# if __name__ == "__main__":
#     l = [1,2,3,4,5]
#     begin = ListNode(l[0])
#     bb = begin
#     for i in range(1,5):
#         bb.next = ListNode(l[i])
#         bb = bb.next
        
#     s = Solution()
#     a = s.rotateRight(begin,2)
    
#     while(a is not None):
#         print(a.val)
#         a = a.next